package com.itheima.job;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

public class Test05 {

}

class IDataOperationImpl implements IDataOperation<Student1>{
    private ArrayList<Student1> arrayList = new ArrayList<>();
    @Override
    public boolean add(Student1 t) {
        if (!(arrayList.isEmpty())) {
            String addId = t.getId();
            for (Student1 stu : arrayList) {
                if (stu.getId().equals(addId)){
                    System.out.println("id已重复");
                    return false;
                }
            }
        }
        arrayList.add(t);
        System.out.println("添加成功");
        return true;
    }

    @Override
    public <T> boolean delete(T t) {
        if (arrayList.isEmpty()) {
            System.out.println("请先添加数据");
            return false;
        }
        Iterator<Student1> it = arrayList.iterator();
        while (it.hasNext()){
            Student1 stu = it.next();
            if (stu.getId().equals(t)){
                it.remove();
                System.out.println("删除成功");
                return true;
            }
        }
        System.out.println("删除失败");
        return false;
    }

    @Override
    public <T> boolean update(Student1 t) {
        if (arrayList.isEmpty()){
            System.out.println("请先添加数据");
            return false;
        }
        for (int i = 0; i < arrayList.size(); i++) {
            if (t.getId().equals(arrayList.get(i).getId())){
                arrayList.set(i,t);
                System.out.println("修改成功");
                return true;
            }
        }
        System.out.println("修改失败");
        return false;
    }

    @Override
    public <T> Student1 get(T t) {
        if (arrayList.isEmpty()){
            System.out.println("请先添加数据");
            return null;
        }
        for (Student1 stu : arrayList) {
            if (stu.getId().equals(t)){
                return stu;
            }
        }
        return null;
    }

    @Override
    public Collection<Student1> getAll() {
        if (arrayList.isEmpty()){
            System.out.println("当前无数据");
            return null;
        }
        return arrayList;
    }
}

//数据操作接口
interface IDataOperation<E> {
    // 添加数据
    public boolean add(E t);
    // 根据唯一标识删除一个数据
    public <T> boolean delete(T t);
    // 修改一个数据
    public <T> boolean update(E e);
    // 根据唯一表示查找一个数据
    public <T> E get(T t);
    // 查找所有数据
    public Collection<E> getAll();
}

// 标准学生类
class Student1 {
    private String id;// 学号
    private String name;// 姓名
    private String age;// 年龄
    private String address;// 居住地
    public Student1() {}
    public Student1(String id, String name, String age, String address) {
        this.id = id;
        this.name = name;
        this.age = age;
        this.address = address;
    }
    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getAge() {
        return age;
    }
    public void setAge(String age) {
        this.age = age;
    }
    public String getAddress() {
        return address;
    }
    public void setAddress(String address) {
        this.address = address;
    }
    @Override
    public String toString() {
        return "Student [id=" + id + ", name=" + name + ", age=" + age + ", address=" + address + "]";
    }
}